package com.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.ajax.AjaxRes;
import com.common.mybatis.Page;
import com.entity.XinWenLanMu;
import com.entity.XinWenZiXun;
import com.service.XinWenLanMuService;
import com.service.XinWenZiXunService;

@Controller
@RequestMapping("/xinwenzixun/manager")
public class XinWenZiXunController {

	@Autowired
	private XinWenZiXunService xwzxService;
	@Autowired
	private XinWenLanMuService xwlmService;
	
	@RequestMapping("/xwzxtable")
	public String table(Model md,String id){
		return "pages/xwzxtable";
	}
	
	@RequestMapping("/findByPage")
	@ResponseBody
	public AjaxRes findByPage(XinWenZiXun xw,Page<XinWenZiXun> page){
		AjaxRes res = new AjaxRes();
		page = xwzxService.findByPage(xw,page);
		res.setSucceed(page,"成功");
		return res;
	}
	@RequestMapping("/addxwzx")
	@ResponseBody
	public AjaxRes addxwzx(XinWenZiXun xw,String fujianId){
		XinWenLanMu o= new XinWenLanMu();
		String lmmc = xw.getLmmc();
		o.setLmmc(lmmc);
		o = xwlmService.findId(o);
		xw.setXwlmbh(o.getId());
		AjaxRes res = new AjaxRes();
		xwzxService.insertFj(xw,fujianId);
		res.setResMsg("成功");
		return res;
	}
	@RequestMapping("/delxwzx")
	@ResponseBody
	public AjaxRes delxwzx(XinWenZiXun xw){
		AjaxRes res = new AjaxRes();
		xwzxService.delete(xw);
		res.setSucceedMsg("删除成功");
		return res;
	}
	
	@RequestMapping("/delAllxwzx")
	@ResponseBody
	public AjaxRes delAllxwzx(String cks){
		AjaxRes res = new AjaxRes(); 
		String[] ck = cks.split(",");
		List<XinWenZiXun> list = new ArrayList<XinWenZiXun>();
		for(String s:ck){
			XinWenZiXun xw = new XinWenZiXun();
			xw.setId(s);
			list.add(xw);
		}
		xwzxService.deleteBatch(list);
		res.setSucceedMsg("删除成功");
		return res;
	}

	
	@RequestMapping("/updatexwzx")
	@ResponseBody
	public AjaxRes updateyhhy(XinWenZiXun xw){
		XinWenLanMu o= new XinWenLanMu();
		String lmmc = xw.getLmmc();
		o.setLmmc(lmmc);
		o = xwlmService.findId(o);
		xw.setXwlmbh(o.getId());
		AjaxRes res = new AjaxRes();
		xwzxService.update(xw);
		res.setSucceedMsg("修改成功");
		return res;
	}

	@RequestMapping("/find")
	@ResponseBody
	public AjaxRes find(XinWenZiXun xw){
		AjaxRes res = new AjaxRes();
		List<XinWenZiXun> xwList = xwzxService.find(xw);
		res.setSucceed(xwList,"获取数据成功");
		return res;
	}
	
	@RequestMapping("/findTree")
	@ResponseBody
	public AjaxRes findTree(XinWenZiXun xw){
		AjaxRes res = new AjaxRes();
		List<XinWenZiXun> xwList = xwzxService.findTree(xw);
		res.setSucceed(xwList,"获取数据成功");
		return res;
	}
	
}
